# Things I do not like:
#   - translation files stay below input, not very clearly



########################
# Basic variables
########################

# name of the project
project:=$(strip $(project))
ifeq ($(project),)
project:=tenacitas
endif

# cpu-os-comp
coc:=$(strip $(coc))
ifeq ($(coc),)
coc:=x86_linux_g++
endif

# major version
M_version:=$(strip $(M_version))
ifeq ($(M_version),)
M_version:=1
endif

# minor version
m_version:=$(strip $(m_version))
ifeq ($(m_version),)
m_version:=0
endif

# build number
build_number:=$(strip $(build_number))
ifeq ($(build_number),)
build_number=1
endif




# cpu
cpu:=$(shell echo $(coc) | cut -d '_' -f 1)

# os 
os:=$(shell echo $(coc) | cut -d '_' -f 2)

# compiler
comp:=$(shell echo $(coc) | cut -d '_' -f 3)

# if 'verbose' is not defined, the first action of the rule (command) is 
# '@echo', which will make the identifier 'echo' not to appear on the 
# terminal, as well as all the bash commands used
verbose:=$(strip $(verbose))
ifeq ($(verbose),1)
show:=echo
make_cmd=make project="$(project)" coc="$(coc)" target="$(target)" verbose="$(verbose)" M_version="$(M_version)" m_version="$(m_version)" build_number="$(build_number)"
else
show:=@echo
# make invocation
make_cmd=make -s project="$(project)" coc="$(coc)" target="$(target)" verbose="$(verbose)" M_version="$(M_version)" m_version="$(m_version)" build_number="$(build_number)"
endif


# defining compiler flags
ifeq ($(target),debug)
compile_flags=$(compile_debug_flags)
else
ifeq ($(target),release)
compile_flags=$(compile_release_flags)
else
compile_flags=$(compile_debug_flags)
target=debug
endif
endif

# This variable defines the extension for the Qt files
qt_extension:=ui_qt


#compile_flags:=$(compile_debug_flags)

#compile_flags:=$(strip $(compile_flags))

# ================ DIRECTORIES ==========================

# directory of the tool used to build the projects
dir_tool=$(PWD)


# ------- global directories

# base directory of local development, this produces <some-path>/tenacitas/cpp
dir_dvp=$(subst tool/gmake,,$(dir_tool))

# base directory for project files
dir_prjs=$(dir_dvp)/prj/gmake

# base directory for programs files
dir_bins=$(dir_dvp)/bin

# base directory for library files
dir_libs=$(dir_dvp)/lib

# base directory for source files
dir_srcs=$(dir_dvp)/src

# base directory for object files
dir_objs:=$(dir_dvp)/obj

# base directory for header files
dir_incs:=$(dir_dvp)/include

# base directory for documentation files
dir_docs:=$(dir_dvp)/doc

# base directory for shared files
dir_shares:=$(dir_dvp)/share

# base directory for configuration files
dir_etcs:=$(dir_dvp)/etc

# base directory for output files
dir_vars:=$(dir_dvp)/var

# base directory for test files
dir_tests:=$(dir_dvp)/test

# ------- project directories

# project directory for project files
dir_prj=$(dir_prjs)/$(project)

# project directories for programs files
dir_bin=$(dir_bins)

# project directories for library files
dir_lib=$(dir_libs)

# project directories for source files
dir_src=$(dir_srcs)/$(project)

# base directory for object files
dir_obj:=$(dir_objs)/$(project)

# project directories for header files
dir_inc:=$(dir_incs)/$(project)

# project directories for documentation files
dir_doc:=$(dir_docs)/$(project)

# project directories for shared files
dir_share:=$(dir_shares)/$(project)

# project directories for configuration files
dir_etc:=$(dir_etcs)/$(project)

# project directories for output files
dir_var:=$(dir_vars)/$(project)

# project directory for test files
dir_test:=$(dir_tests)/$(project)



# avoiding error for non-existance of '.deps' file
#$(shell touch $(dir_prj)/.deps)

# identifiers of the possible types of project
library=lib
program=pgm
just_copy_files=just_copy
module=mod

# this is for the 'make info_cmd cmd=<cmd>' command
# if no <cmd> is defined, we suggest 'info'
cmd:=$(strip $(cmd))
ifeq ($(cmd),)
cmd="info"
endif


# accessing the definitions for this project
-include $(dir_prj)/project.mk


# accessing the definitions for the cpu-os-comp combination
-include $(dir_tool)/cpu_os_comp/$(cpu)_$(os)_$(comp).mk


# including the calculated rules, based on the dependencies of the source files 
# to header files
-include $(dir_src)/.*_deps

# right margin for output messages
line_formater=fmt -w 80 -u -s

#######
# basic rules
#######

# Shows the basic commands (rules) 
default:
	$(show) ""                                                           ; \
	echo "------------------------------------------------------------"  ; \
	echo "Bacic commands:"                                               ; \
	echo "------------------------------------------------------------"  ; \
	echo ""                                                              ; \
	option="    make tutorial: shows a tutorial how to use the 'tenacitas' build system." ; \
	echo $$option | $(line_formater)                                     ; \
	echo ""                                                              ; \
	option="    make cmd_list: lists all the avaiable commands with a brief explanation." ; \
	echo $$option | $(line_formater)                                     ; \
	echo ""                                                              ; \
	option="    make cmd_info cmd=<cmd_name>: shows detailed information for a command." ; \
	echo $$option | $(line_formater)                                     ; \
	echo ""                                                              ; \
	option="    make coc_list: lists the values of the variables for a CPU, OS, compiler combination" ; \
	echo $$option | $(line_formater)                                     ; \
	echo ""                                                              ; \
	option="    make prj_list: lists all the avaiable Projects."         ; \
	echo $$option | $(line_formater)                                     ; \
	echo ""                                                              ; \


# Shows a tutorial that explains how the 'tenacitas' build system works 
tutorial:
	$(show) ""                                                          ; \
	cat $(dir_root)/introduction.txt | $(line_formater) | less          ;






